package com.bafa.middle.betwixt;

import com.bafa.jdbc.DBAccess;
import com.bafa.objects.player.Player;
import com.bafa.objects.schema.Match;
import com.bafa.objects.team.TeamStats;

import java.io.StringReader;
import java.io.StringWriter;

import java.sql.PreparedStatement;

import org.apache.commons.betwixt.io.BeanReader;
import org.apache.commons.betwixt.io.BeanWriter;
import org.apache.commons.betwixt.io.read.BeanCreationList;
import org.apache.commons.betwixt.io.read.ChainedBeanCreatorFactory;
import org.apache.commons.betwixt.strategy.NameMapper;

public class Utils {
    public Utils() {
    }
    
    
    public static String writeBetwixt(String element,Object obj)throws Exception{
        // Start by preparing the writer
        // We'll write to a string 
        StringWriter outputWriter = new StringWriter(); 
        
        // Betwixt just writes out the bean as a fragment
        // So if we want well-formed xml, we need to add the prolog
        outputWriter.write("<?xml version='1.0' ?>");
        
		
        // Create a BeanWriter which writes to our prepared stream
        BeanWriter beanWriter = new BeanWriter(outputWriter);
        
        // Configure betwixt
        // For more details see java docs or later in the main documentation
        beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
        beanWriter.getBindingConfiguration().setMapIDs(false);
        beanWriter.enablePrettyPrint();

        // If the base element is not passed in, Betwixt will guess 
        // But let's write example bean as base element 'person'
        beanWriter.write(element, obj);
        return outputWriter.toString();
        
    }
    
    public static void betwixtMatch(Match match)throws Exception{
        DBAccess db = new DBAccess();
        /*******************************BETWIXT IT ALL********************************/
        com.bafa.objects.Match betwixtMatch = new com.bafa.objects.Match();
        
        betwixtMatch.setAwayAdded(match.isAwayAdded());
        betwixtMatch.setHomeAdded(match.isHomeAdded());
        betwixtMatch.setTeamA(match.getHomeTeam());
        betwixtMatch.setTeamB(match.getAwayTeam());
        betwixtMatch.setBlocked(match.isBlocked());
        betwixtMatch.setGoalsA(match.getHomeGoals());
        betwixtMatch.setGoalsB(match.getAwayGoals());
        betwixtMatch.setTime(match.getTime());
        betwixtMatch.setID(match.getID());
        betwixtMatch.setConfirmed(match.isConfirmed());
        betwixtMatch.setHomeAdded(betwixtMatch.isHomeAdded());
        betwixtMatch.setAwayAdded(match.isAwayAdded());
        betwixtMatch.setPlayed(match.isPlayed());
        betwixtMatch.setDivision(match.getDivision());
        betwixtMatch.setSeasonId(match.getSeasonId());
        betwixtMatch.setTeamAName(match.getHomeTeamName());
        betwixtMatch.setTeamBName(match.getAwayTeamName());
        if(match.getDate()!=null)betwixtMatch.setDate(match.getDate().toGregorianCalendar().getTime());
        for(int i=0;i<match.getHomeScorer().size();i++){
             betwixtMatch.addScorersA(match.getHomeScorer().get(i).getName(),match.getHomeScorer().get(i).getGoals());
        }
             
        for(int i=0;i<match.getAwayScorer().size();i++){
             betwixtMatch.addScorersB(match.getAwayScorer().get(i).getName(),match.getAwayScorer().get(i).getGoals());
        }
             
        PreparedStatement updateTeam = db.conn.prepareStatement("insert into matchesbetwixt values(?,?)");
        updateTeam.setObject(2, writeBetwixt("match",betwixtMatch).getBytes());  
        updateTeam.setInt(1, match.getID());
        updateTeam.executeUpdate();
                 
                 
           /**********************************************BETWIXT IT ALL******************************************/   
        
        
    }
    
    public static Object readBetwixt(String xml,String element,Class cls){
            Object matchBetwixt = null;
        try {
            
            
            // First construct the xml which will be read in
                   // For this example, read in from a hard coded string
                   StringReader xmlReader = new StringReader(xml);
                   
                   // Now convert this to a bean using betwixt
                   // Create BeanReader
                   BeanReader beanReader  = new BeanReader();
                   // Configure the reader
                   // If you're round-tripping, make sure that the configurations are compatible!
				   
                   beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
                   beanReader.getBindingConfiguration().setMapIDs(false);
                   // Register beans so that betwixt knows what the xml is to be converted to
                   // Since the element mapped to a PersonBean isn't called the same 
                   // as Betwixt would have guessed, need to register the path as well
                   beanReader.registerBeanClass(element, cls);
				   BeanCreationList beanList = new BeanCreationList();
				   //beanList.insertBeanCreator(0,ChainedBeanCreatorFactory.createDerivedBeanCreator());
				  // beanReader.getReadConfiguration().setBeanCreationChain(beanList);
	                //beanReader.registerBeanClass(TeamStats.class);
                   // Now we parse the xml
                   matchBetwixt = beanReader.parse(xmlReader);
                   
                  
        }catch(Exception e){e.printStackTrace();}
            return matchBetwixt;
        }
           
       
   
}
